package com.zs.toolbox.web2j.system.core.controller.admin;


import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.common.exception.WtResponses;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPageQuerySearchModel;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAdminBaseController;
import com.zs.toolbox.web2j.system.core.entity.WtDictionaryItemEntity;
import com.zs.toolbox.web2j.system.core.service.WtDictionaryItemService;
import com.zs.toolbox.web2j.system.core.service.WtDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-08
 */
@RestController
@RequestMapping(value = "/api/v1/admin/dictionary/item")
@Api("公共字典项管理接口")
public class WtAdminDictionaryItemController extends WtAdminBaseController<WtDictionaryItemService, WtDictionaryItemEntity,String> {


    @Autowired
    WtDictionaryTypeService wtDictionaryTypeService;

    @Override
    public WtBaseResponse<WtPageBodyModel<WtDictionaryItemEntity>> getPage(@RequestBody WtPageQuerySearchModel wtPageQueryModel) {
        wtPageQueryModel.setSearchKey(getTableField(WtDictionaryItemEntity.class,wtPageQueryModel.getSearchKey()));
        return super.getPage(wtPageQueryModel);
    }

    @PostMapping("insertFile")
    @ApiOperation("批量导入字典信息，格式 value\\ttext\\tsort 其中value为数字，text为任意字符,sort为数字")
    WtBaseResponse insertByFile(@RequestPart String dictType, MultipartFile file) {
        if(StringUtils.isBlank(dictType) || null == wtDictionaryTypeService.getById(dictType)) {
            throw WtExceptions.E_DICT_KEY;
        }
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(file.getInputStream()));

            List<WtDictionaryItemEntity> saveDictItems = bufferedReader.lines().flatMap(line -> {
                String[] split = line.split("\\s*");
                if (split.length > 2) {
                    String value = split[0];
                    String text = split[1];
                    String order = split[2];
                    return Stream.of(new WtDictionaryItemEntity()
                            .setText(text).setTypeId(dictType)
                            .setSort(Integer.valueOf(order))
                            .setValue(Integer.valueOf(value)));
                } else {
                    return Stream.empty();
                }
            }).collect(Collectors.toList());
            bufferedReader.close();
            baseService.saveBatch(saveDictItems);
        } catch (IOException e) {
            throw WtExceptions.E_DICT_FILE_UPLOAD;
        }
        return WtResponses.SUCCESS;
    }

}
